package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.*;
import com.example.demo.mapper.*;
import com.example.demo.service.AdminService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
@AllArgsConstructor
public class AdminServiceImpl implements AdminService {

    private final UserMapper userMapper;
    private final GoodsMapper goodsMapper;
    private final OrdersManagementMapper ordersMapper;
    private final CommentsMapper commentsMapper;

    // 分页
    @Override
    public IPage<User> getUserPage(int page, int size, String keyword) {
        Page<User> pageParam = new Page<>(page, size);
        return userMapper.selectUserPage(pageParam, keyword);
    }

    @Override
    public IPage<Goods> getGoodsPage(int page, int size, String keyword) {
        Page<Goods> pageParam = new Page<>(page, size);
        return goodsMapper.selectGoodsPage(pageParam, keyword);
    }

    @Override
    public IPage<OrdersManagement> getOrdersPage(int page, int size, String keyword) {
        Page<OrdersManagement> pageParam = new Page<>(page, size);
        return ordersMapper.selectOrdersPage(pageParam, keyword);
    }

    @Override
    public IPage<Comments> getCommentsPage(int page, int size, String keyword) {
        Page<Comments> pageParam = new Page<>(page, size);
        return commentsMapper.selectCommentsPage(pageParam, keyword);
    }

    // 用户管理
    @Override
    @Transactional
    public boolean deleteUser(Integer userId) {
        // 检查用户是否存在
        User user = userMapper.selectById(userId);
        if (user == null) {
            return false;
        }

        // 执行删除
        return userMapper.deleteById(userId) > 0;
    }

    @Override
    public User getUserDetails(Integer userId) {
        User user = userMapper.selectById(userId);

        // 确保所有字段都有值
        if (user != null) {
            if (user.getUBalance() == null) {
                user.setUBalance(BigDecimal.ZERO);
            }
            if (user.getUBio() == null) {
                user.setUBio("");
            }
        }

        return user;
    }

    // 商品管理
    @Override
    @Transactional
    public boolean deleteGoods(Integer goodsId) {
        // 标记为下架而非直接删除
        Goods goods = goodsMapper.selectById(goodsId);
        if (goods != null) {
            goods.setGIsActive(false);
            return goodsMapper.updateById(goods) > 0;
        }
        return false;
    }

    @Override
    public Goods getGoodsDetails(Integer goodsId) {
        Goods goods = goodsMapper.selectById(goodsId);
        if (goods != null) {
            // 确保必填字段不为空
            if (goods.getGPrice() == null) {
                goods.setGPrice(BigDecimal.ZERO);
            }
            if (goods.getGStockQuantity() == null) {
                goods.setGStockQuantity(0);
            }
            if (goods.getGDescription() == null) {
                goods.setGDescription("");
            }
        }
        return goods;
    }

    // 订单管理
    @Override
    @Transactional
    public boolean deleteOrder(Integer orderId) {
        return ordersMapper.deleteById(orderId) > 0;
    }

    @Override
    public OrdersManagement getOrderDetails(Integer orderId) {
        OrdersManagement order = ordersMapper.selectById(orderId);
        if (order != null) {
            // 确保必填字段不为空
            if (order.getOTotalAmount() == null) {
                order.setOTotalAmount(BigDecimal.ZERO);
            }
            if (order.getOStatus() == null) {
                order.setOStatus("未知状态");
            }
        }
        return order;
    }

    // 评论管理
    @Override
    @Transactional
    public boolean deleteComment(Integer commentId) {
        return commentsMapper.deleteById(commentId) > 0;
    }

    @Override
    public Comments getCommentDetails(Integer commentId) {
        return commentsMapper.selectById(commentId);
    }

    // 个人管理
    @Override
    @Transactional
    public boolean updateUserBio(Integer userId, String bio) {
        return userMapper.updateUserBio(userId, bio) > 0;
    }

    @Override
    @Transactional
    public boolean changePassword(Integer userId, String oldPassword, String newPassword) {
        // 验证旧密码
        User user = userMapper.selectById(userId);
        if (user == null || !user.getUPassword().equals(oldPassword)) {
            return false;
        }
        return userMapper.updateUserPassword(userId, newPassword) > 0;
    }

    @Override
    @Transactional
    public BigDecimal rechargeBalance(Integer userId, BigDecimal amount) {
        userMapper.rechargeBalance(userId, amount);
        // 返回新的余额
        return userMapper.selectById(userId).getUBalance();
    }
}